package com.leetcode.partition7;

import java.io.*;
import java.util.*;

/**
 * @author `RKC`
 * @date 2022/2/16 10:00
 */
public class LC699掉落的方块 {

    private static final int N = 10010;
    private static Set<Integer> set = new TreeSet<>();
    private static Map<Integer, Integer> map = new HashMap<>();
    private static Node[] tr = new Node[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(reader.readLine());
        int[][] positions = new int[n][2];
        for (int i = 0; i < n; i++) {
            String[] ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            positions[i] = new int[]{a, b};
        }
        writer.write(fallingSquares(positions) + "\n");
        writer.flush();
    }

    public static List<Integer> fallingSquares(int[][] positions) {
        //有序离散化
        for (int[] position : positions) {
            set.add(position[0]);
            set.add(position[0] + position[1] - 1);
        }
        int cnt = 0;
        for (int coord : set) map.put(coord, ++cnt);
        //创建线段树，维护一个区间最大高度
        build(1, 1, set.size() << 1);
        List<Integer> answer = new ArrayList<>();
        int maxh = 0;
        for (int[] pos : positions) {
            //根据离散化后的区间值查询区间最大高度
            int l = map.get(pos[0]), r = map.get(pos[0] + pos[1] - 1), h = query(1, l, r);
            maxh = max(maxh, h + pos[1]);
            answer.add(maxh);
            modify(1, l, r, h + pos[1]);
        }
        return answer;
    }

    private static int query(int u, int l, int r) {
        if (l <= tr[u].l && r >= tr[u].r) return tr[u].h;
        pushdown(u);
        int mid = tr[u].l + tr[u].r >> 1;
        if (r <= mid) return query(u << 1, l, r);
        if (l > mid) return query(u << 1 | 1, l, r);
        return max(query(u << 1, l, r), query(u << 1 | 1, l, r));
    }

    private static void modify(int u, int l, int r, int val) {
        if (l <= tr[u].l && r >= tr[u].r) {
            tr[u].h = max(tr[u].h, val);
            tr[u].flag = val;
        } else {
            pushdown(u);
            int mid = tr[u].l + tr[u].r >> 1;
            if (l <= mid) modify(u << 1, l, r, val);
            if (r > mid) modify(u << 1 | 1, l, r, val);
            pushup(u);
        }
    }

    private static void pushdown(int u) {
        if (tr[u].flag != 0) {
            tr[u << 1].h = max(tr[u << 1].h, tr[u].h);
            tr[u << 1].flag = max(tr[u << 1].flag, tr[u].flag);
            tr[u << 1 | 1].h = max(tr[u << 1 | 1].h, tr[u].h);
            tr[u << 1 | 1].flag = max(tr[u << 1 | 1].flag, tr[u].flag);
            tr[u].flag = 0;
        }
    }

    private static void pushup(int u) {
        tr[u].h = max(tr[u << 1].h, tr[u << 1 | 1].h);
    }

    private static void build(int u, int l, int r) {
        tr[u] = new Node(l, r, 0, 0);
        if (l != r) {
            int mid = l + r >> 1;
            build(u << 1, l, mid);
            build(u << 1 | 1, mid + 1, r);
            pushup(u);
        }
    }

    private static int max(int a, int b) {
        return Math.max(a, b);
    }

    private static class Node {
        private int l, r;
        private int h, flag;

        public Node(int l, int r) {
            this.l = l;
            this.r = r;
        }

        public Node(int l, int r, int h, int flag) {
            this.l = l;
            this.r = r;
            this.h = h;
            this.flag = flag;
        }
    }
}
